package zyy;

import java.sql.Connection;
import java.sql.PreparedStatement;

public class SaveMysql {
  // private static final String driver = "com.mysql.cj.jdbc.Driver";
  private static final String url = "jdbc:mysql://47.93.197.229:3306/mqtt?useUnicode=true&characterEncoding=utf8&useSSL=false";
  private static final String user = "mqtt";
  private static final String password = "zucc511";
  private static Connection conn = null;

  public static void savedate(String topic, int qos, String str) throws Exception {
    getConnection();

    String sql = "insert into IOT_ZYY(Topic,Qos,Payload) values(?,?,?)";
    PreparedStatement pst = conn.prepareStatement(sql);
    pst = conn.prepareStatement(sql);
    pst.setString(1, topic);
    pst.setInt(2, qos);
    pst.setString(3, str);
    if (pst.executeUpdate() == 1) {
      System.out.println("--------------------insert----------------------");
      System.out.printf("Topic: %s\nQos: %d\nPayload: %s\n", topic, qos, str);
      System.out.println("--------------------success---------------------");
    } else {
      throw new Exception("添加失败");
    }
    pst.close();
    conn.close();
  }

  public static void getConnection() throws java.sql.SQLException {
    if (conn == null) {
      conn = java.sql.DriverManager.getConnection(url, user, password);
    }
  }

  public static void main(String[] args) {
    try {
      savedate(args[0], Integer.parseInt(args[1]), args[2]);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}